package embyparse.tokenizer;

import java.util.Enumeration;
import java.util.Hashtable;

import embyparse.noobparser.Constants;

public class MultiCharacterOperators {

	private static String[] MULTICHAROPS = Constants.MULTICHAROPS;
	private static Hashtable<String, String> convertDictionary = new Hashtable<String,String>();
	private static Hashtable<String, String> revertDictionary = new Hashtable<String,String>();
	static{
		populateDictionaries();
	}

	private static void populateDictionaries(){
		for( int i=0; i<MULTICHAROPS.length; i++){
			add( MULTICHAROPS[i], (char)(900+i));
		}
	}
	private static void add(Object op1, Object op2){
		convertDictionary.put((String)op1,  (new Character((Character)op2)).toString());
		revertDictionary.put((new Character((Character)op2)).toString(), (String)op1);
	}

	/**
	 * Returns the same string except with multicharacter ops replaced with their respective single-character codes.<BR>
	 * Called in preparse
	 * @param s
	 * @return
	 */
	public static String convert(String cheese){
		Enumeration<String> e = convertDictionary.keys();
		while( e.hasMoreElements()){
			String s = e.nextElement();
			cheese = cheese.replace( s,  convertDictionary.get(s));
		}
		return cheese;
	}

	/**
	 * Opposite of convert <BR> Plus, spaces out the operators
	 * Called in tokenize
	 * @param s
	 * @return
	 */
	public static String revert(String cheese){
		Enumeration<String> e = revertDictionary.keys();
		while( e.hasMoreElements()){
			String s = e.nextElement();
			cheese = cheese.replace(s,  " "+revertDictionary.get(s)+" ");
		}
		return cheese;
	}


}
